﻿using System.Windows;
using System.Windows.Controls;
using appSupplier.UserControls;
using appDataContracts;
using System;
using System.Windows.Threading;
using System.IO;
using Microsoft.Win32;
using System.Windows.Media.Imaging;


namespace appSupplier
{
    /// <summary>
    /// Logique d'interaction pour CreateCatalogWindow.xaml
    /// </summary>
    public partial class CreateCatalogWindow : Window
    {
        private UCCatalogs callerUC;
        private bool edit;
        private int catalogId;
        private Stream stream = null;
        private string fileName = string.Empty;

        public CreateCatalogWindow(UCCatalogs callerUC, bool edit)
        {
            InitializeComponent();
            this.edit = edit;

            #region Pré-remplissage des infos
            if (edit)
            {
                lblTop.Content = "Modification du catalogue";
                btValidate.Content = "Enregistrer";
                Title = "Modification";
                tbName.Text = ((SerializedCatalog)callerUC.dgCatalog.SelectedItem).CATALOGNAME;
                tbDescription.Text = ((SerializedCatalog)callerUC.dgCatalog.SelectedItem).CATALOGDESCRIPTION;
                tbImagePath.Text = ((SerializedCatalog)callerUC.dgCatalog.SelectedItem).CATALOGIMAGE;
                catalogId = ((SerializedCatalog)callerUC.dgCatalog.SelectedItem).CATALOGID;
            } 
            #endregion

            if (!edit)
            {
                this.Width = 351;
            }
            else
            {
                #region Chargement image
                try
                {

                    myPictureServ.PictureServiceClient client = new myPictureServ.PictureServiceClient();
                    SerializedPictureFile file = client.Download(tbImagePath.Text);
                    Stream s = new MemoryStream(file.PictureStream);

                    img.Source = BitmapFrame.Create(s, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                }
                catch (Exception)
                {

                }
                #endregion
            }

            this.callerUC = callerUC;
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btValidate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tbName.Text.Trim() == "")
                {
                    lblError.Content = "Nom requis";
                }
                else
                {
                    if (!edit)
                    {
                        string imgPath = tbImagePath.Text;
                        #region upload d'image
                        if (stream != null)
                        {
                            byte[] bytes = new byte[stream.Length];
                            stream.Read(bytes, 0, (int)stream.Length);
                            SerializedPictureFile file = new SerializedPictureFile();
                            file.PictureStream = bytes;
                            file.PictureName = fileName;

                            myPictureServ.PictureServiceClient client = new myPictureServ.PictureServiceClient();
                            client.Upload(file);

                            //http://www.silverlightshow.net/items/Uploading-and-downloading-images-from-WCF-in-Silverlight.aspx
                        }
                        #endregion

                        if (imgPath.Trim() == "")
                        {
                            imgPath = "catalogdefault.png";
                        }

                        bool test = AppWindow.supplierService.addCatalog(new SerializedCatalog(0, AppWindow.supplier.SUPPLIERID, tbDescription.Text, tbName.Text, imgPath));
                    }
                    else
                    {
                        string imgPath = tbImagePath.Text;
                        #region upload d'image
                        if (stream != null)
                        {
                            byte[] bytes = new byte[stream.Length];
                            stream.Read(bytes, 0, (int)stream.Length);
                            SerializedPictureFile file = new SerializedPictureFile();
                            file.PictureStream = bytes;
                            file.PictureName = fileName;

                            myPictureServ.PictureServiceClient client = new myPictureServ.PictureServiceClient();
                            client.Upload(file);
                        }
                        #endregion

                        if (imgPath.Trim() == "")
                        {
                            imgPath = "productdefault.png";
                        }
                        bool test = AppWindow.supplierService.updateCatalog(new SerializedCatalog(catalogId, AppWindow.supplier.SUPPLIERID, tbDescription.Text, tbName.Text, imgPath));
                    }
                    callerUC.update();
                    this.Close();
                }
            }
            catch (System.Exception ex)
            {
                
            }
        }

        private void tbImagePath_GotMouseCapture(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Dispatcher.BeginInvoke(
                new Action(
                delegate
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    openFileDialog.Filter = "Images |*.jpg; *.png; *.gif";
                    if (openFileDialog.ShowDialog() == true)
                    {
                        stream = (Stream)openFileDialog.OpenFile();
                        fileName = openFileDialog.SafeFileName;
                        tbImagePath.Text = fileName;
                    }
                }),
                DispatcherPriority.ApplicationIdle);
        }
    }
}
